package com.project.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.project.demo.controller.base.BaseController;
import com.project.demo.entity.ReportInformation;
import com.project.demo.entity.vo.*;
import com.project.demo.service.ReportInformationService;
import com.project.demo.util.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 举报信息：(ReportInformation)表控制层
 */
@Slf4j
@RestController
@RequestMapping("/report_information")
public class ReportInformationController extends BaseController<ReportInformation, ReportInformationService> {

    private static final String llmToken = "Bearer MWedycUFPEMYytNcUxMa:ACVUbLjjYPTlyejRrcDf";

    private static final String llmUrl = "https://spark-api-open.xf-yun.com/v1/chat/completions";

    /**
     * 举报信息对象
     */
    @Autowired
    public ReportInformationController(ReportInformationService service) {
        setService(service);
    }

    @PostMapping("/add")
    @Transactional
    public Map<String, Object> add(HttpServletRequest request) throws IOException {
        Map<String, Object> paramMap = service.readBody(request.getReader());
        this.addMap(paramMap);
        return success(1);
    }

    /**
     * 智能审核
     */
    @GetMapping("/audit")
    @Transactional
    public Map<String, Object> audit(HttpServletRequest request, String content) throws NoSuchAlgorithmException, KeyManagementException {
        HashMap<String, Object> resultMap = new HashMap<>();
        BasicHeader tokenHeader = new BasicHeader("Authorization", llmToken);
        BasicHeader contentTypeHeader = new BasicHeader("Content-Type", "application/json");
        LlmPostDto llmPostDto = new LlmPostDto();
        llmPostDto.setModel("lite");
        llmPostDto.setStream(false);
        Messages messages = new Messages();
        messages.setContent(content + " " + "识别以上文本中的错别字，返回50字以内的结果");
        messages.setRole("user");
        ArrayList<Messages> messagesList = new ArrayList<>();
        messagesList.add(messages);
        llmPostDto.setMessages(messagesList);
        JSONObject jsonObject = HttpClientUtils.httpsPost(llmUrl, JSON.toJSONString(llmPostDto), tokenHeader, contentTypeHeader);
        log.info("大模型返回结果:{}", jsonObject);
        if ((Integer) jsonObject.get("code") == 0) {
            LlmResultVo llmResultVo = jsonObject.toJavaObject(LlmResultVo.class);
            Choice choice = llmResultVo.getChoices().get(0);
            Delta delta = choice.getMessage();
            String resultContent = delta.getContent();
            resultMap.put("message", resultContent);
            return resultMap;
        }
        resultMap.put("message", "未识别出错误");
        return resultMap;
    }


}
